#include "qfaceobject.h"
#include<qDebug>

QFaceObject::QFaceObject(QObject *parent) : QObject(parent)
{
    // 初始化加载模型
    seeta::ModelSetting FDmodel("F:/Project/arm+opencv+linux/output/face/bin/model/fd_2_00.dat",
                                seeta::ModelSetting::CPU,0);
    seeta::ModelSetting PDmodel("F:/Project/arm+opencv+linux/output/face/bin/model/pd_2_00_pts5.dat",
                                seeta::ModelSetting::CPU,0);
    seeta::ModelSetting FRmodel("F:/Project/arm+opencv+linux/output/face/bin/model/fr_2_10.dat",
                                seeta::ModelSetting::CPU,0);
    // 建立人脸识别引擎
    this->fengineptr = new seeta::FaceEngine(FDmodel,PDmodel,FRmodel);
    this->fengineptr->Load("./face.db");
}

QFaceObject::~QFaceObject()
{
    delete fengineptr;
}

int64_t QFaceObject::face_register(cv::Mat &faceImage)
{
    //opencv的mat数据转为seeta face的数据
    SeetaImageData simage;
    simage.data = faceImage.data;
    simage.width = faceImage.cols;
    simage.height = faceImage.rows;
    simage.channels = faceImage.channels();

    //注册
    int64_t faceId = this->fengineptr->Register(simage);
    if(faceId >= 0)
    {
        fengineptr->Save("./face.db");
    }
    return faceId;
}

int QFaceObject::face_query(cv::Mat &faceImage)
{
    SeetaImageData simage;
    simage.data = faceImage.data;
    simage.width = faceImage.cols;
    simage.height = faceImage.rows;
    simage.channels = faceImage.channels();

    float similarity = 0;
    int64_t faceId = fengineptr->Query(simage,&similarity);  //查询时间比较长
    qDebug()<<"查询"<<faceId<<similarity;

    if(similarity > 0.7){  // 置信度>0.7
        emit send_faceid(faceId);
    }else{
        emit send_faceid(-1);
    }
    return faceId;
}
